<?php

namespace App\Admin\Controllers\Bureau;

use App\Admin\Actions\Grid\UsedCommentsPassAction;
use App\Admin\Repositories\Bureau\WxBureauComment;
use App\Wen\Utils\Settings;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;

class WxBureauCommentController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new WxBureauComment(), function (Grid $grid) {
            $grid->model()->orderBy('id', 'desc');
            $grid->column('id')->sortable();
            $grid->quickSearch(['id', 'comment_content'])->placeholder('搜索ID，评论内容...')->width(50);
            $grid->column('owers', '头像')->display(function ($v) {
                return '<img src="' . ($this->ower['user_avatar'] ?? Settings::get('img_default', 'https://img.mini.minisns.cn/icons/dafault.png'))  . '"  style="border-radius:50px;width:30px;" />';
            })->width('50px');
            $grid->column('ower', '用户名称')->display(function ($v) {
                if($v && $v['user_name']){
                    return '<a target="_blank" href="'.admin_url('users?id='.$v['id']).'">'.$v['user_name'].'</a>' ?? '用户已删除';
                }
                return '用户已删除';
            })->width('100px');
            $grid->column('used_good_id');
            $grid->column('comment_id');
            $grid->column('comment_agent_id');
            $grid->column('comment_agent_name');
            $grid->column('comment_content')->limit(40);
            $grid->column('comment_img_url')->image('', 60, 60);
//            $grid->column('longitude');
//            $grid->column('latitude');
//            $grid->column('ip');
//            $grid->column('country');
//            $grid->column('province');
//            $grid->column('city');
//            $grid->column('district');
            $grid->column('comment_state', '状态')->using([0 => '待审核', 1 => '审核通过', 2 => '驳回'])->label([
                0 => 'red',
                1 => 'green',
                2 => 'default'
            ]);
//            $grid->column('created_at');
            $grid->column('updated_at')->sortable();

            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');
            });

            $grid->batchActions(function ($batch) {
                $batch->add(new UsedCommentsPassAction());
            });

            $grid->disableCreateButton();

        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new WxBureauComment(), function (Show $show) {
            $show->field('id');
            $show->field('user_id');
            $show->field('used_good_id');
            $show->field('comment_id');
            $show->field('comment_agent_id');
            $show->field('comment_agent_name');
            $show->field('comment_content');
            $show->field('comment_img_url');
            $show->field('longitude');
            $show->field('latitude');
            $show->field('ip');
            $show->field('country');
            $show->field('province');
            $show->field('city');
            $show->field('district');
            $show->field('comment_state');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new WxBureauComment(), function (Form $form) {
            $form->display('id');
//            $form->text('user_id');
//            $form->text('used_good_id');
//            $form->text('comment_id');
//            $form->text('comment_agent_id');
//            $form->text('comment_agent_name');
            $form->textarea('comment_content')->required();
//            $form->image('comment_img_url')->url('files/uploads')->uniqueName()->autoUpload();
//            $form->text('longitude');
//            $form->text('latitude');
//            $form->text('ip');
//            $form->text('country');
//            $form->text('province');
//            $form->text('city');
//            $form->text('district');
            $form->radio('comment_state', '状态')->options([0=>'审核中' , 1=> '审核通过', 2 =>'驳回']);

//            $form->display('created_at');
//            $form->display('updated_at');

            $form->deleting(function (Form $form){
                global $__MINI_GLOBAL_TENANT_ID__;
                if($__MINI_GLOBAL_TENANT_ID__ > 0){
                    return $form->response()->error('权限不足，不可以删除其他分站对象');
                }
            });

        });
    }
}
